﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages.Profile
{
    public class CommentAdd : YouHP.Pages.Profile.ProfileBasePage
    {
        public override void InitPage()
        {
            base.InitPage();
            PageKey = PageKeys.Profile_CommentAdd;
        }

        protected CommentInfo ReplyCommentData;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            this.SetTitle("发表留言");
            this.AddLocation("发表留言", "");

            if (IsPost)
                HandlePost();
            else HandleGet();
        }

        private void HandleGet()
        {
            int rid = RequestTools.GetQueryInt("rid", 0);
            if (rid <= 0)
            {
                this.AddErrLine("无效的参数rid");
                return;
            }
            ReplyCommentData = CommentBLL.Instance.GetCommentInfo(rid);
            if (ReplyCommentData.Id <= 0)
            {
                this.AddErrLine("不存在的评论");
                return;
            }

        }

        protected void HandlePost()
        {
            if (UserData.Id <= 0)
            {
                this.AddErrLine("请先登录");
                this.SetUrl("/User/Login.aspx");
                this.SetMetaRefresh();
                return;
            }
            CommentInfo info = ValidataForm();
            if (info == null) return;
            bool succ = CommentBLL.Instance.AddComment(info);

            string url;
            PageKeys pageid = PageHelper.GetReferrerPageIdentify();
            if (pageid == PageKeys.Profile_Index)
            {
                url = string.Format("/Profile/Index.aspx?uid={0}", ProfileData.Id);
            }
            else if (pageid == PageKeys.Profile_CommentList)
            {
                url = string.Format("/Profile/CommentList.aspx?uid={0}", ProfileData.Id);
            }
            else
            {
                url = string.Format("/Profile/Index.aspx?uid={0}", ProfileData.Id);
            }


            if (succ)
            {
                this.Redirect(url);
                return;
            }
            else
            {
                this.AddErrLine("留言发表失败");
                this.SetUrl(url);
                this.SetMetaRefresh();
                return;
            }
        }

        CommentInfo ValidataForm()
        {
            CommentInfo info = new CommentInfo();
            info.HostUserId = ProfileData.Id;

            info.Body = RequestTools.GetFormString("msg").Trim();
            if (info.Body.Length < 2)
            {
                this.AddErrLine("留言长度不能小于 2 ");
                return null;
            }
            info.UpdateTime = DateTime.Now;
            info.PostTime = DateTime.Now;
            info.PostUserId = UserData.Id;
            info.PostUserName = UserData.UserName;

            return info;
        }
    }
}